package gao.xiaolei.dao;

import gao.xiaolei.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository("UserDao")
public interface UserDao extends JpaRepository<User, String>, JpaSpecificationExecutor {

	// JPA已经实现了这个方法
	public User findByUsername(String username);
	
	@Query(value = "select id from User  where username =:userName")
	public String getUserId(@Param("userName") String userName);

	@Modifying
	@Query(value = "update User set password =:password where id =:id")
	public void updatePassword(@Param("id") String id,
                               @Param("password") String password);

	@Modifying
	@Query(value = "update User set username =:newUserName where id =:id")
	public void updateUserName(@Param("newUserName") String newUserName,
                               @Param("id") String id);

	@Modifying
	@Query(value = "update User set realName =:newRealName where id =:id")
	public void updateRealName(@Param("newRealName") String RealName,
                               @Param("id") String id);

	@Query(value = "select count(*) from User  where username =:userName")
	public int getUserNameCount(@Param("userName") String userName);

	@Query(value = "select username from User  where id =:userId")
	public String getUserName(@Param("userId") String userId);
	
	@Query(value = "select realName from User  where id =:userId")
	public String getRealName(@Param("userId") String userId);

	@Query(value = "select password from User  where username =:userName")
	public String getPassword(@Param("userName") String userName);

}
